package com.huanxi.joytravel.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.huanxi.joytravel.JoyTravelApplication;
import com.huanxi.joytravel.R;


/**
 * Toast 工具类
 * 解决Toast连续出现要等前一个时间到
 * 再显示下一个的延迟现象
 *
 * @author DB
 */
public class ToastUtil {


    private static Toast mToast;
    private static Toast mImgToast;

    /**
     * @param message
     * @param duration
     * @return
     */
    private static Toast initToast(CharSequence message, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(JoyTravelApplication.getInstance(), message, duration);
        } else {
            mToast.setText(message);
            mToast.setDuration(duration);
        }
        return mToast;
    }

    /**
     * 短时间显示Toast
     *
     * @param strResId
     */
    public static void showShort(int strResId) {
        initToast(JoyTravelApplication.getInstance().getResources().getText(strResId), Toast
         .LENGTH_SHORT).show();
    }

    public static void showErrorMsg(String msg) {
        initToast(msg, Toast.LENGTH_SHORT).show();
    }

    public static void showErrorMsg(int strResId) {
        initToast(JoyTravelApplication.getInstance().getResources().getText(strResId), Toast
         .LENGTH_SHORT).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param message
     */
    public static void showLong(CharSequence message) {
        initToast(message, Toast.LENGTH_LONG).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param strResId
     */
    public static void showLong(int strResId) {
        initToast(JoyTravelApplication.getInstance().getResources().getText(strResId), Toast
         .LENGTH_LONG).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param message
     * @param duration
     */
    public static void show(CharSequence message, int duration) {
        initToast(message, duration).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param strResId
     * @param duration
     */
    public static void show(Context context, int strResId, int duration) {
        initToast(context.getResources().getText(strResId), duration).show();
    }

    public static void showNormalDialog(Context mContext, String msg) {
        final AlertDialog.Builder normalDialog =
         new AlertDialog.Builder(mContext);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("温馨提示:");
        normalDialog.setMessage(msg);
        normalDialog.setNegativeButton("关闭",
         new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 //...To-do
             }
         });
        // 显示
        normalDialog.show();
    }

    public static Toast showNorToast(final String tvStr) {
        if (mImgToast == null) {
            mImgToast = new Toast(JoyTravelApplication.getInstance());
        }
        View view = LayoutInflater.from(JoyTravelApplication.getInstance()).inflate(R.layout
         .new_toast_custom, null);
        TextView tv = view.findViewById(R.id.tv_toast);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.setDuration(Toast.LENGTH_LONG);
        mImgToast.show();
        return mImgToast;
    }

    public static Toast showNorToast(final int strResId) {
        if (mImgToast == null) {
            mImgToast = new Toast(JoyTravelApplication.getInstance());
        }
        View view = LayoutInflater.from(JoyTravelApplication.getInstance()).inflate(R.layout
         .new_toast_custom, null);
        TextView tv = view.findViewById(R.id.tv_toast);
        tv.setText(TextUtils.isEmpty(JoyTravelApplication.getInstance().getResources().getText
         (strResId)) ? "" : JoyTravelApplication.getInstance().getResources().getText(strResId));
        mImgToast.setView(view);
        mImgToast.setGravity(Gravity.CENTER, 0, 0);
        mImgToast.setDuration(Toast.LENGTH_LONG);
        mImgToast.show();
        return mImgToast;
    }
}
